package com.googletran;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;

public class SmartDictionary extends Activity{
	
	private Button btngoogletran =  null;
	private Button btnDictionary = null;
	private Button btnHelp = null;
	private Button btnSetup = null;
	private Button btnOption = null;
	
	static final private String MAIN_TAG = "[Smart Dictionary]";

	
	static final private int SHOW_PREFERENCE_CODE = 2;	
			
	
	//private Button btnWikiOffline = null;	
	/////>>>>>>>>>>>
	
	public void openAbout(String strAboutPath, boolean bType)
	{
		setContentView(R.layout.about);
	    WebView wvInfo = (WebView) findViewById(R.id.wvInfo);
		
		if (bType == false) // about of dictionary
		{
			String strLine;
			StringBuilder sbInfo = new StringBuilder();
			Log.i(MAIN_TAG,"About path = " + strAboutPath);
			File fInfo = new File(strAboutPath);
			if (fInfo.exists() && fInfo.length() < 8096)
			{
				try{
				    BufferedReader br = new BufferedReader(new FileReader(fInfo));
					//Read File Line By Line
					while ((strLine = br.readLine()) != null)   {
						sbInfo.append(strLine);
					}
					
					//Close the input stream
					br.close();
				}
				catch (Exception e)
				{//Catch exception if any
					sbInfo.append("<html><body><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
					//sbInfo.append(mDBFile.fileName);
					sbInfo.append("</body></html>");
				}				
			}
			else
			{
				Log.i(MAIN_TAG,"About file doesn't exist!");
				sbInfo.append("<html><body><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
				//sbInfo.append(mDBFile.fileName);
				sbInfo.append("</body></html>");
			}
			wvInfo.loadDataWithBaseURL (null, sbInfo.toString(), "text/html", "UTF-8","about:blank");
		}
		else
		{
			wvInfo.loadUrl(strAboutPath);
		}

		Button btnOkAbout = (Button) findViewById(R.id.btnOkAbout);
        btnOkAbout.setOnClickListener(new OnClickListener()
        {
        	public void onClick(View v)
        	{
                setContentView(R.layout.main);
                //if (mDBList.items.size() > 0)
               // {
                 //   menuMain();
                //}
        	}
        });
		
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);		
		setContentView(R.layout.main);
		btngoogletran = (Button)findViewById(R.id.btn_google);
		btngoogletran.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
			
				Intent i = new Intent(SmartDictionary.this,GoogletranActivity.class);
				startActivity(i);
				
			}
		});
		btnDictionary = (Button)findViewById(R.id.btn_dictionary);
		btnDictionary.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
			
				Intent i = new Intent(SmartDictionary.this,Dictionary.class);
				startActivity(i);				
			}
		});
		btnHelp = (Button)findViewById(R.id.btn_help);
		btnHelp.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				openAbout("file:///android_asset/about.html",true);
			}
		});
		btnSetup = (Button)findViewById(R.id.btn_setup);
		btnSetup.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {				
				startActivityForResult(new Intent(SmartDictionary.this, Preference.class), SHOW_PREFERENCE_CODE);
			}
		}); 
		btnOption = (Button)findViewById(R.id.btn_DictonaryOption);
		btnOption.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				//Intent i = new Intent(SmartDictionary.this,DictionaryManager.class);
				//startActivity(i);
			}
		});
	}

}
